﻿using System;
using System.Web.WebPages;

public static class WebMatrixHelpers
{
    public static ErrorMessage Error(this WebPageBase web, string message)
    {
        var error = new ErrorMessage(message);
        if (error.IsError)
        {
            web.Page.IsValid = false;
        }
        return error;
    }

	public static void Init(this WebPageBase web, string layout, string title, Action<WebPageBase> action=null)
	{
        web.Layout = layout;
        web.Page.Title = title;
        web.Page.IsValid = true;
        if (action != null)
        {
            action(web);
        }
	}
}

public class ErrorMessage
{
    readonly string message;

    public bool IsError { get { return !string.IsNullOrWhiteSpace(message); } }
    public string Message { get { return message; } }

    public ErrorMessage(string message)
    {
        this.message = message;
    }
    public static implicit operator string(ErrorMessage error)
    {
        if (error == null) return null;
        return error.message;
    }

    static readonly ErrorMessage nil = new ErrorMessage(null);
    public static ErrorMessage Nil
    {
        get
        {
            return nil;
        }
    }
}